#include<bits/stdc++.h>
#define ll long long
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define Root 1,1,n
#define OK l>=Ll&&r<=Rr
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define pb push_back
#define ull unsigned long long
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned int 
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57) s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
const int N =1e6+5,M=4e5+5,inf=2147000000;
const double eps=1e-6;
const ll mod=1e9+7,llf=1e18;
using namespace std;
inline ll qp(int a,int b){
	if(!b)return 1;
	ll c=qp(a,b>>1);
	c=c*c%mod;
	if(b&1)c=c*a%mod;
	return c;
}
int n;ll ans[N];
struct node{
	int l,r,f;
}a[N],b[N];
inline void rotate(int x){
	if(!b[x].f)return;
	int fa=b[x].f,gran=b[b[x].f].f;
	if(fa==b[gran].l)b[gran].l=x;
	else b[gran].r=x;
	if(x==b[fa].l)b[fa].l=b[x].r,b[x].f=gran,b[fa].f=x,b[x].r=fa;
	else b[fa].r=b[x].l,b[x].f=gran,b[fa].f=x,b[x].l=fa;
}
inline void splay(int x){
	while(b[x].f){
		//cerr<<x<<":"<<b[x].f<<"\n";
		if(!b[b[x].f].f){
			rotate(x);
			return;
		}
		int fa=b[x].f,gran=b[b[x].f].f;
		bool f=(x==b[fa].l)^(fa==b[gran].l);
		if(f)rotate(x);
		else rotate(fa);
		rotate(x);
	}
}
inline void dfs(int x,int dep){
	//if(x==5)cerr<<dep<<" ";
	//cerr<<x<<":"<<b[x].l<<" "<<b[x].r<<'\n';
	ans[x]=(ans[x]+dep)%mod;
	if(b[x].l)dfs(b[x].l,dep+1);
	if(b[x].r)dfs(b[x].r,dep+1);
}
inline void subt1(){
	rep(i,1,n){
		rep(j,1,n)b[j]=a[j];
		splay(i);
		dfs(i,0);
	}
	rep(i,1,n)pf(ans[i]),putchar('\n');
}
inline void Dfs(int x,int dep){
	ans[x]=(ll)dep*(ll)(dep+1)/2LL%mod;
	int k=n-dep-1;
	if(dep%2==0){
		ans[x]=(ans[x]+(ll)k/2LL*(dep/2+2)%mod+(ll)(k-k/2)*(dep/2+1)%mod)%mod;
	}
	else {
		ans[x]=(ans[x]+(ll)k/2LL*(dep/2+3)%mod+(ll)(k-k/2)*(dep/2+1)%mod)%mod;
	}
	if(a[x].l)Dfs(a[x].l,dep+1);
}
int main(){
	freopen("dream.in","r",stdin);
	freopen("dream.out","w",stdout);
	n=read();
	rep(i,1,n)a[i].l=read(),a[i].r=read(),a[a[i].l].f=a[a[i].r].f=i;
	if(n<=2000){
		subt1();
		return 0;
	}
	rep(i,1,n)if(!a[i].f)Dfs(i,0);
	rep(i,1,n)pf(ans[i]),putchar('\n');
	return 0;
}
